#include "../include/acceptor.h"
#include "../include/tcpConnection.h"
#include "../include/eventLoop.h"
#include "../include/tcpServer.h"
#include "../include/threadPool.h"
#include <unistd.h>
#include <iostream>
#include <functional>

using std::cout;
using std::bind;

ThreadPool *gPool = nullptr;

class Task
{
public:
	Task(const string & msg,const shared_ptr<TcpConnection> & con)
	:_msg(msg)
	,_con(con)
	{


	}
	
	void process()
	{
		_con->sendInLoop(_msg);
	}

private:
	string _msg;
	
	shared_ptr<TcpConnection> _con;
};

void callBackNewConnection(const shared_ptr<TcpConnection> & con)
{
	cout << con->toString() << " 加入连接!\n ";
}

void callBackMessage(const shared_ptr<TcpConnection> & con)
{
	string msg = con->receive();

	cout << ">>客户端发来消息: " << msg << "\n";

	Task task(msg,con);

	gPool->addTask(bind(&Task::process,task));
}

void callBackClose(const shared_ptr<TcpConnection> & con)
{
	cout << con->toString() << " 断开连接！\n";
}

int main()
{
	ThreadPool pool(2,4);

	pool.start();
	gPool = &pool;

	TcpServer tcpServer("127.0.0.1",8888);

	tcpServer.setAllCallBack(std::move(callBackNewConnection),std::move(callBackMessage),std::move(callBackClose));

	tcpServer.start();

	return 0;
}












